import { Injectable } from '@angular/core';

@Injectable()
export class ProductService {
  private products: Array<Product> =[
    new Product(1,"第一个商品",1.99,3.5,"这是第一个商品",["电子产品","硬件设备"]),
    new Product(2,"第二个商品",2.99,2.5,"这是第二个商品",["电子产品"]),
    new Product(3,"第三个商品",3.99,1.5,"这是第三个商品",["硬件设备"]),
    new Product(4,"第四个商品",4.99,4.5,"这是第四个商品",["图书"]),
    new Product(5,"第五个商品",5.99,3.5,"这是第五个商品",["医药"]),
    new Product(6,"第六个商品",6.99,2.5,"这是第六个商品",["美妆"]),
  ];
  private comments: Comment[] = [
    new Comment(1,1,"2017-12-24",'lixiangwei',3,"东西不错"),
    new Comment(2,1,"2017-12-24",'张三',3,"东西还行"),
    new Comment(3,2,"2017-12-24",'李四',5,"我很喜欢"),
    new Comment(4,2,"2017-12-24",'王五',1,"差评"),

  ];
  constructor() { }
  getProducts() {
    return this.products;
  }
  getProduct(id: number): Product  {
   return this.products.find(product => product.id === id);
  }
  getCommentsForProductId(id: number):Comment[] {
     return this.comments.filter((comment: Comment) => comment.productId == id);
  }
  //所有商品分类
  getAllCategries() {
    return ["电子产品", "硬件设备", "图书", "医药", "美妆" ];
  }

}
export class Product {
  constructor(
    public id:number,
    public title:string,
    public price:number,
    public rating:number,
    public desc:string,
    public category:Array<string>
  ) {
    
  }
}
export class Comment {
  constructor(
    public id: number,
    public productId: number,
    public timestamp: string,
    public user: string,
    public rating:number,
    public content:string,
  ) {
    
  }
}